package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_message;

import cn.com.api.service.ApiT_messageService;

import cn.com.util.*;
import cn.com.util.*;


public class T_messageBaseController {

	private static Logger logger = Logger.getLogger(T_messageBaseController.class);
	
	@Autowired
	private ApiT_messageService apiT_messageService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_message-list")
	public ModelAndView t_message_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_message_list.jsp");
		
		List<ApiT_message> t_messages = apiT_messageService.findT_messages();
		mv.addObject("t_messages", t_messages);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_message-add-pre")
	public ModelAndView t_message_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_message_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_message t_message = apiT_messageService.findT_messageById(Integer.valueOf(id));
			mv.addObject("t_message",t_message);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_message-add")
	public void t_message_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String orderid = request.getParameter("orderid");
		String shop_id = request.getParameter("shop_id");
		String u_id = request.getParameter("u_id");
		String messagetype = request.getParameter("messagetype");
		String is_send = request.getParameter("is_send");
		String status = request.getParameter("status");
		String inserttime = request.getParameter("inserttime");
		String operatetime = request.getParameter("operatetime");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_message t_message = new ApiT_message();
            /*
		    t_message.setId(id);
		    t_message.setOrderid(orderid);
		    t_message.setShop_id(shop_id);
		    t_message.setU_id(u_id);
		    t_message.setMessagetype(messagetype);
		    t_message.setIs_send(is_send);
		    t_message.setStatus(status);
		    t_message.setInserttime(inserttime);
		    t_message.setOperatetime(operatetime);
		    t_message.setTitle(title);
		    t_message.setContent(content);
		    t_message.setExt_1(ext_1);
		    t_message.setExt_2(ext_2);
		    t_message.setExt_3(ext_3);
		    t_message.setExt_4(ext_4);
		    t_message.setExt_5(ext_5);
	        */
			apiT_messageService.insertT_message(t_message);
		}
		//edit
		else{
			ApiT_message t_message = apiT_messageService.findT_messageById(Integer.valueOf(id));
	        /*
		    t_message.setId(id);
		    t_message.setOrderid(orderid);
		    t_message.setShop_id(shop_id);
		    t_message.setU_id(u_id);
		    t_message.setMessagetype(messagetype);
		    t_message.setIs_send(is_send);
		    t_message.setStatus(status);
		    t_message.setInserttime(inserttime);
		    t_message.setOperatetime(operatetime);
		    t_message.setTitle(title);
		    t_message.setContent(content);
		    t_message.setExt_1(ext_1);
		    t_message.setExt_2(ext_2);
		    t_message.setExt_3(ext_3);
		    t_message.setExt_4(ext_4);
		    t_message.setExt_5(ext_5);
			*/
			apiT_messageService.updateT_message(t_message);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_message-delete")
	public void t_message_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_messageService.deleteT_message(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



